#ifndef MAEMO5QTPROJECTGENERATOR_H
#define MAEMO5QTPROJECTGENERATOR_H

#include <QMap>
#include <QObject>
#include <QSet>
#include <QString>
#include <QStack>
#include <QProcess>

#include "defines.h"
#include "generatordata.h"
#include "generatoroutputview.h"

#include "iqtprojectgenerator.h"

class Maemo5ProcessRunner;

class Maemo5QtProjectGenerator : public IQtProjectGenerator
{
    Q_OBJECT

protected:
    //
    // protected members
    //

    GeneratorOutputView   & m_outputView;
    GeneratorData           m_generatorData;
    Maemo5ProcessRunner   * m_processRunner;
    GeneratorPhase          m_currentGeneratorPhase;
    GeneratorTarget         m_generatorTarget;
    // TODO ? QStack<QString>         m_pluginDirs;
    WidgetType              m_resolvedWidgetType;

    QString                 m_appName;
    QString                 m_appDisplayName;

    QMap<QString, QString>  m_filenameSubstitutions;
    QSet<QString>           m_ignorePatterns;
    QMap<QString, QString>  m_keywordSubstitutions;

public:
    //
    // lifetime management
    //
    Maemo5QtProjectGenerator(GeneratorOutputView & outputView,
                             QObject             * parent = NULL);
    virtual ~Maemo5QtProjectGenerator();

    //
    // public operators
    //
signals:
    void started(GeneratorTarget target);
    void finished(int exitCode);

public:
    virtual IStatusUpdater * createStatusUpdater(QObject * parent = NULL);

    virtual const PlatformType& platform();

    virtual void build(const QString &widgetFile,
                       RcSession     & /* rcSession */);
    virtual void rebuild(RcSession     & /* rcSession */);
    virtual void preview(const QString &widgetFile);
    virtual void closeProject();
    virtual GeneratorOutputView &outputView() const;
    virtual GeneratorData &generatorData();
    virtual GeneratorPhase getCurrentPhase() const;
    virtual GeneratorTarget getTarget() const;
    virtual QString getStatusText() const;

    virtual QWidget * createSettingsWidget(const QString & widgetFile,
                                           QWidget       * parent = NULL) const;
    virtual void saveSettings(QWidget * settingsWidget) const;
    virtual QStringList errorsForTarget(GeneratorTarget generatorTarget) const;

    virtual PkgInstallResult installPkg(const QFileInfo & pkgFile) const;


protected slots:

    virtual void dpkgBuildPackageFinished(int exitCode,
                                          QProcess::ExitStatus exitStatus);
    virtual void widgetFilesCopied(int exitCode,
                                   QProcess::ExitStatus exitStatus = QProcess::NormalExit);

protected:
    // OBS virtual Maemo5ProcessRunner * createProcessRunner();

    void copyWidgetDirectory(const QString &wgzFile); // Uncompress widget file
    bool prepareProject();
    bool prepareEnvironment(const QString &wgzFile);
    void unzipWidgetFile(const QString &wgzFile);
    bool registerStatus(bool status);
    void sniffWidgetType();
    bool copyFrameworkAndPluginSrcFiles();
    bool prepareEnvironmentForNonWRTWidget();
    bool handleWidgetConfigFile();
    bool copyHybridApiJs();
    bool copyDisableSelectionCSS();
    void handleWidgetDirectory();
    bool updateMainFile();
    bool updateMainWindowFile();
    void parseWidgetResourceFile();
    bool updateFrameworkProjectFile();

    bool copyMaemo5Files();
    bool copyCopyrightFile();

    virtual void startBuild();

    /** OBS
      * Synchronously unzips a widget file - returns with success.
      * /
    bool syncUnzipWidgetFile(const QString & wgzFile,
                             const QString & unzipDir);
    void sniffAppNames(const QString & widgetFile);
    */
    void updateSubstitutions(const QString & widgetFile);
};

#endif // MAEMO5QTPROJECTGENERATOR_H
